/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eng.scc.core.editors;

import com.eng.scc.core.data.Device;
import com.eng.scc.core.parts.IntegerTextField;
import com.eng.scc.core.util.Error;
import java.awt.Component;
import java.awt.Dimension;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author Rafael
 */
public class DeviceEditor extends AbstractPanelEditor<Device>
{

    public static void main( String[] args )
    {
        try
        {
            DeviceEditor.edit( null, new Device() );
        }
        catch ( Exception ex )
        {
            Logger.getLogger( DeviceEditor.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }
    
    /**
     * Creates new form NewJPanel
     */
    public DeviceEditor()
    {
        initComponents();
    }

    public static boolean edit( Component parent, Device d ) throws Exception
    {
        DeviceEditor ce = new DeviceEditor();

        ce.setSource( d );
        
        ce.populateFields();
        
        boolean acpt = DefaultEditor.createEditor( parent, 
                                           "Editor de Dispositivos", 
                                           "Informe os dados do dispositivo instalado no veículo", 
                                           new Dimension( 360, 400), ce );
        
        return acpt;
    }

    @Override
    public void setSource( Device source )
    {
        if ( source == null )
        {
            throw new IllegalArgumentException( "Vehicle cannot be null!" );
        }

        this.source = source;
    }
    
    private void populateFields()
    {
        tfSerial.setValue( source.getSerial() );
        tfDateInstalation.setDate( source.getInstallationDate() );
        infoPane.setText( source.getDescription() );
    }

    @Override
    public void getSource()
    {
        source.setSerial( tfSerial.getValue() );
        
        if ( tfDateInstalation.getDate() != null )
        {
            source.setInstallationDate( new Date( tfDateInstalation.getDate().getTime() ) );
        }
        
        source.setDescription( infoPane.getText() );
    }

    @Override
    public void validateEditor( Error error )
    {
        if ( tfSerial.getValue() <= 0 )
        {
            error.add( "Informe o Serial do dispositivo que pertence a este veículo!" );
        }
    }
    
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfSerial = new IntegerTextField();
        tfDateInstalation = new JXDatePicker();
        jScrollPane1 = new javax.swing.JScrollPane();
        infoPane = new javax.swing.JTextPane();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Serial:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Data Instalação:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Informações Adicionais:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfSerial, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfDateInstalation, gridBagConstraints);

        jScrollPane1.setViewportView(infoPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jScrollPane1, gridBagConstraints);
    }

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private IntegerTextField tfSerial;
    private JXDatePicker tfDateInstalation;
    private javax.swing.JTextPane infoPane;
}
